<?php

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class decorateModel extends Model
{
    use SoftDeletes;

    protected $table = 'decorate';
    protected $primaryKey = 'id';

    protected function serializeDate($date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    //获取当前平台模板装修数据
    public static function getCurrentPlatformDecorate($platform = '', $id = 0)
    {
        $where = ['deleted_at' => null];
        if ($id !== 0) {
            $where['dianpu_id'] = $id;
        }
        if ($platform == 'preview') {
            $decorate = self::where($where)->orderBy('id')->first();
        } else {
            $decorate = self::whereRaw('find_in_set(?,platform)', [$platform])
                ->where('status', 'normal')
                ->where('type', 'shop')
                ->orderBy('id')
                ->first();
        }
        if (!$decorate) {
            throw new Exception('未找到模板');
        }
        $template = self::getDecorateContent($decorate);
        return $template;
    }

    public static function getCustomDecorate($id)
    {
        $decorateContent = decoratecontentModel::where('decorate_id', $id)->get();
        foreach ($decorateContent as &$v) {
            $v['content'] = self::getDecorateContentByType($v);
        }
        return $decorateContent;
    }

    public static function getDecorateContent($decorate)
    {
        $template['home'] = [];           //首页
        $template['user'] = [];           //个人中心
        $template['tabbar'] = [];         //底部菜单
        $template['popup'] = [];          //弹出提醒
        $template['float-button'] = [];   //悬浮按钮
        $decorateContent = decoratecontentModel::where('decorate_id', $decorate->id)->get();
        foreach ($decorateContent as $k => $v) {
            $v['content'] = self::getDecorateContentByType($v);
            if (!is_null($v['content'])) {
                $template[$v->category][] = $v;
            }
        }
        return $template;
    }

    public static function getDecorateContentByType($templateValue)
    {
        $content = json_decode($templateValue->content, true);
        switch ($templateValue->type) {
            case 'search':
            case 'coupons':
            case 'live':
            case 'groupon':
            case 'wallet-card':
            case 'order-card':
            case 'rich-text':
            case 'nav-bg':
                break;
            case 'seckill':
                $seckill = activityModel::where('id', $content['id'])->where('status', 1)->first();
                $content = $seckill ? $content : null;
                break;
            case 'banner':
            case 'menu':
            case 'adv':
            case 'grid-list':
            case 'popup':
            case 'nav-list':
                foreach ($content['list'] as &$l) {
                    $l['image'] && $l['image'] = $l['image'];
                }
                break;
            case 'user':
            case 'title-block':
            case 'goods-list':
            case 'goods-group':
                $content['image'] && $content['image'] = $content['image'];
                break;
            case 'tabbar':
                if ($content['style'] < 3) {
                    foreach ($content['list'] as &$l) {
                        $l['image'] && $l['image'] = $l['image'];
                        $l['activeImage'] && $l['activeImage'] = $l['activeImage'];
                    }
                }
                break;
            case 'float-button':
                $content['image'] = $content['image'];
                foreach ($content['list'] as &$l) {
                    if ($l['style'] == 1) {
                        $l['image'] && $l['image'] = $l['image'];
                    }
                    $l['btnimage'] && $l['btnimage'] = $l['btnimage'];
                }
        }
        return $content;
    }

}
